{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import yaml\n",
    "import datetime\n",
    "import numpy as np\n",
    "import keras\n",
    "import keras.backend as K\n",
    "import tensorflow as tf\n",
    "from voc_generator import PascalVocGenerator, ImageSetLoader\n",
    "from keras.callbacks import (\n",
    "    ReduceLROnPlateau,\n",
    "    CSVLogger,\n",
    "    EarlyStopping,\n",
    "    ModelCheckpoint,\n",
    "    TerminateOnNaN)\n",
    "from keras_tqdm import TQDMNotebookCallback\n",
    "\n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\"\n",
    "\n",
    "global _SESSION\n",
    "config = tf.ConfigProto(allow_soft_placement=True)\n",
    "config.gpu_options.allow_growth = True\n",
    "_SESSION = tf.Session(config=config)\n",
    "K.set_session(_SESSION)\n",
    "\n",
    "\n",
    "with open(\"init_args.yml\", 'r') as stream:\n",
    "    try:\n",
    "        init_args = yaml.load(stream)\n",
    "    except yaml.YAMLError as exc:\n",
    "        print(exc)\n",
    "\n",
    "checkpointer = ModelCheckpoint(\n",
    "    filepath=\"output/checkpoint_fcn_densenet.h5\",\n",
    "    verbose=1,\n",
    "    save_best_only=True)\n",
    "lr_reducer = ReduceLROnPlateau(monitor='val_loss',\n",
    "                               factor=np.sqrt(0.1),\n",
    "                               cooldown=0,\n",
    "                               patience=10, min_lr=1e-12)\n",
    "early_stopper = EarlyStopping(monitor='val_loss',\n",
    "                              min_delta=0.001,\n",
    "                              patience=30)\n",
    "nan_terminator = TerminateOnNaN()\n",
    "csv_logger = CSVLogger(\n",
    "    #'output/{}_fcn_vgg16.csv'.format(datetime.datetime.now().isoformat()))\n",
    "    'output/tmp_fcn_densenet.csv')\n",
    "\n",
    "datagen = PascalVocGenerator(image_shape=[224, 224, 3],\n",
    "                                    image_resample=True,\n",
    "                                    pixelwise_center=True,\n",
    "                                    pixel_mean=[115.85100, 110.50989, 102.16182],\n",
    "                                    pixelwise_std_normalization=True,\n",
    "                                    pixel_std=[70.30930, 69.41244, 72.60676])\n",
    "\n",
    "train_loader = ImageSetLoader(**init_args['image_set_loader']['train'])\n",
    "val_loader = ImageSetLoader(**init_args['image_set_loader']['val'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "____________________________________________________________________________________________________\n",
      "Layer (type)                     Output Shape          Param #     Connected to                     \n",
      "====================================================================================================\n",
      "input_1 (InputLayer)             (None, 224, 224, 3)   0                                            \n",
      "____________________________________________________________________________________________________\n",
      "initial_conv2D (Conv2D)          (None, 224, 224, 48)  1296        input_1[0][0]                    \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_1 (BatchNorm (None, 224, 224, 48)  192         initial_conv2D[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_1 (Activation)        (None, 224, 224, 48)  0           batch_normalization_1[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_1 (Conv2D)                (None, 224, 224, 16)  6912        activation_1[0][0]               \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_1 (Concatenate)      (None, 224, 224, 64)  0           initial_conv2D[0][0]             \n",
      "                                                                   conv2d_1[0][0]                   \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_2 (BatchNorm (None, 224, 224, 64)  256         concatenate_1[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "activation_2 (Activation)        (None, 224, 224, 64)  0           batch_normalization_2[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_2 (Conv2D)                (None, 224, 224, 16)  9216        activation_2[0][0]               \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_2 (Concatenate)      (None, 224, 224, 80)  0           concatenate_1[0][0]              \n",
      "                                                                   conv2d_2[0][0]                   \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_3 (BatchNorm (None, 224, 224, 80)  320         concatenate_2[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "activation_3 (Activation)        (None, 224, 224, 80)  0           batch_normalization_3[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_3 (Conv2D)                (None, 224, 224, 16)  11520       activation_3[0][0]               \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_3 (Concatenate)      (None, 224, 224, 96)  0           concatenate_2[0][0]              \n",
      "                                                                   conv2d_3[0][0]                   \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_4 (BatchNorm (None, 224, 224, 96)  384         concatenate_3[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "activation_4 (Activation)        (None, 224, 224, 96)  0           batch_normalization_4[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_4 (Conv2D)                (None, 224, 224, 16)  13824       activation_4[0][0]               \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_4 (Concatenate)      (None, 224, 224, 112) 0           concatenate_3[0][0]              \n",
      "                                                                   conv2d_4[0][0]                   \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_5 (BatchNorm (None, 224, 224, 112) 448         concatenate_4[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "activation_5 (Activation)        (None, 224, 224, 112) 0           batch_normalization_5[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_5 (Conv2D)                (None, 224, 224, 112) 12544       activation_5[0][0]               \n",
      "____________________________________________________________________________________________________\n",
      "average_pooling2d_1 (AveragePool (None, 112, 112, 112) 0           conv2d_5[0][0]                   \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_6 (BatchNorm (None, 112, 112, 112) 448         average_pooling2d_1[0][0]        \n",
      "____________________________________________________________________________________________________\n",
      "activation_6 (Activation)        (None, 112, 112, 112) 0           batch_normalization_6[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_6 (Conv2D)                (None, 112, 112, 16)  16128       activation_6[0][0]               \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_5 (Concatenate)      (None, 112, 112, 128) 0           average_pooling2d_1[0][0]        \n",
      "                                                                   conv2d_6[0][0]                   \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_7 (BatchNorm (None, 112, 112, 128) 512         concatenate_5[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "activation_7 (Activation)        (None, 112, 112, 128) 0           batch_normalization_7[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_7 (Conv2D)                (None, 112, 112, 16)  18432       activation_7[0][0]               \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_6 (Concatenate)      (None, 112, 112, 144) 0           concatenate_5[0][0]              \n",
      "                                                                   conv2d_7[0][0]                   \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_8 (BatchNorm (None, 112, 112, 144) 576         concatenate_6[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "activation_8 (Activation)        (None, 112, 112, 144) 0           batch_normalization_8[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_8 (Conv2D)                (None, 112, 112, 16)  20736       activation_8[0][0]               \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_7 (Concatenate)      (None, 112, 112, 160) 0           concatenate_6[0][0]              \n",
      "                                                                   conv2d_8[0][0]                   \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_9 (BatchNorm (None, 112, 112, 160) 640         concatenate_7[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "activation_9 (Activation)        (None, 112, 112, 160) 0           batch_normalization_9[0][0]      \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_9 (Conv2D)                (None, 112, 112, 16)  23040       activation_9[0][0]               \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_8 (Concatenate)      (None, 112, 112, 176) 0           concatenate_7[0][0]              \n",
      "                                                                   conv2d_9[0][0]                   \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_10 (BatchNor (None, 112, 112, 176) 704         concatenate_8[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "activation_10 (Activation)       (None, 112, 112, 176) 0           batch_normalization_10[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_10 (Conv2D)               (None, 112, 112, 176) 30976       activation_10[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "average_pooling2d_2 (AveragePool (None, 56, 56, 176)   0           conv2d_10[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_11 (BatchNor (None, 56, 56, 176)   704         average_pooling2d_2[0][0]        \n",
      "____________________________________________________________________________________________________\n",
      "activation_11 (Activation)       (None, 56, 56, 176)   0           batch_normalization_11[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_11 (Conv2D)               (None, 56, 56, 16)    25344       activation_11[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_9 (Concatenate)      (None, 56, 56, 192)   0           average_pooling2d_2[0][0]        \n",
      "                                                                   conv2d_11[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_12 (BatchNor (None, 56, 56, 192)   768         concatenate_9[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "activation_12 (Activation)       (None, 56, 56, 192)   0           batch_normalization_12[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_12 (Conv2D)               (None, 56, 56, 16)    27648       activation_12[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_10 (Concatenate)     (None, 56, 56, 208)   0           concatenate_9[0][0]              \n",
      "                                                                   conv2d_12[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_13 (BatchNor (None, 56, 56, 208)   832         concatenate_10[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_13 (Activation)       (None, 56, 56, 208)   0           batch_normalization_13[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_13 (Conv2D)               (None, 56, 56, 16)    29952       activation_13[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_11 (Concatenate)     (None, 56, 56, 224)   0           concatenate_10[0][0]             \n",
      "                                                                   conv2d_13[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_14 (BatchNor (None, 56, 56, 224)   896         concatenate_11[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_14 (Activation)       (None, 56, 56, 224)   0           batch_normalization_14[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_14 (Conv2D)               (None, 56, 56, 16)    32256       activation_14[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_12 (Concatenate)     (None, 56, 56, 240)   0           concatenate_11[0][0]             \n",
      "                                                                   conv2d_14[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_15 (BatchNor (None, 56, 56, 240)   960         concatenate_12[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_15 (Activation)       (None, 56, 56, 240)   0           batch_normalization_15[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_15 (Conv2D)               (None, 56, 56, 240)   57600       activation_15[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "average_pooling2d_3 (AveragePool (None, 28, 28, 240)   0           conv2d_15[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_16 (BatchNor (None, 28, 28, 240)   960         average_pooling2d_3[0][0]        \n",
      "____________________________________________________________________________________________________\n",
      "activation_16 (Activation)       (None, 28, 28, 240)   0           batch_normalization_16[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_16 (Conv2D)               (None, 28, 28, 16)    34560       activation_16[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_13 (Concatenate)     (None, 28, 28, 256)   0           average_pooling2d_3[0][0]        \n",
      "                                                                   conv2d_16[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_17 (BatchNor (None, 28, 28, 256)   1024        concatenate_13[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_17 (Activation)       (None, 28, 28, 256)   0           batch_normalization_17[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_17 (Conv2D)               (None, 28, 28, 16)    36864       activation_17[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_14 (Concatenate)     (None, 28, 28, 272)   0           concatenate_13[0][0]             \n",
      "                                                                   conv2d_17[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_18 (BatchNor (None, 28, 28, 272)   1088        concatenate_14[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_18 (Activation)       (None, 28, 28, 272)   0           batch_normalization_18[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_18 (Conv2D)               (None, 28, 28, 16)    39168       activation_18[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_15 (Concatenate)     (None, 28, 28, 288)   0           concatenate_14[0][0]             \n",
      "                                                                   conv2d_18[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_19 (BatchNor (None, 28, 28, 288)   1152        concatenate_15[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_19 (Activation)       (None, 28, 28, 288)   0           batch_normalization_19[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_19 (Conv2D)               (None, 28, 28, 16)    41472       activation_19[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_16 (Concatenate)     (None, 28, 28, 304)   0           concatenate_15[0][0]             \n",
      "                                                                   conv2d_19[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_20 (BatchNor (None, 28, 28, 304)   1216        concatenate_16[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_20 (Activation)       (None, 28, 28, 304)   0           batch_normalization_20[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_20 (Conv2D)               (None, 28, 28, 304)   92416       activation_20[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "average_pooling2d_4 (AveragePool (None, 14, 14, 304)   0           conv2d_20[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_21 (BatchNor (None, 14, 14, 304)   1216        average_pooling2d_4[0][0]        \n",
      "____________________________________________________________________________________________________\n",
      "activation_21 (Activation)       (None, 14, 14, 304)   0           batch_normalization_21[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_21 (Conv2D)               (None, 14, 14, 16)    43776       activation_21[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_17 (Concatenate)     (None, 14, 14, 320)   0           average_pooling2d_4[0][0]        \n",
      "                                                                   conv2d_21[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_22 (BatchNor (None, 14, 14, 320)   1280        concatenate_17[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_22 (Activation)       (None, 14, 14, 320)   0           batch_normalization_22[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_22 (Conv2D)               (None, 14, 14, 16)    46080       activation_22[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_18 (Concatenate)     (None, 14, 14, 336)   0           concatenate_17[0][0]             \n",
      "                                                                   conv2d_22[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_23 (BatchNor (None, 14, 14, 336)   1344        concatenate_18[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_23 (Activation)       (None, 14, 14, 336)   0           batch_normalization_23[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_23 (Conv2D)               (None, 14, 14, 16)    48384       activation_23[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_19 (Concatenate)     (None, 14, 14, 352)   0           concatenate_18[0][0]             \n",
      "                                                                   conv2d_23[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_24 (BatchNor (None, 14, 14, 352)   1408        concatenate_19[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_24 (Activation)       (None, 14, 14, 352)   0           batch_normalization_24[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_24 (Conv2D)               (None, 14, 14, 16)    50688       activation_24[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_20 (Concatenate)     (None, 14, 14, 368)   0           concatenate_19[0][0]             \n",
      "                                                                   conv2d_24[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_25 (BatchNor (None, 14, 14, 368)   1472        concatenate_20[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_25 (Activation)       (None, 14, 14, 368)   0           batch_normalization_25[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_25 (Conv2D)               (None, 14, 14, 368)   135424      activation_25[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "average_pooling2d_5 (AveragePool (None, 7, 7, 368)     0           conv2d_25[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_26 (BatchNor (None, 7, 7, 368)     1472        average_pooling2d_5[0][0]        \n",
      "____________________________________________________________________________________________________\n",
      "activation_26 (Activation)       (None, 7, 7, 368)     0           batch_normalization_26[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_26 (Conv2D)               (None, 7, 7, 16)      52992       activation_26[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_21 (Concatenate)     (None, 7, 7, 384)     0           average_pooling2d_5[0][0]        \n",
      "                                                                   conv2d_26[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_27 (BatchNor (None, 7, 7, 384)     1536        concatenate_21[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_27 (Activation)       (None, 7, 7, 384)     0           batch_normalization_27[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_27 (Conv2D)               (None, 7, 7, 16)      55296       activation_27[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_22 (Concatenate)     (None, 7, 7, 400)     0           concatenate_21[0][0]             \n",
      "                                                                   conv2d_27[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_28 (BatchNor (None, 7, 7, 400)     1600        concatenate_22[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_28 (Activation)       (None, 7, 7, 400)     0           batch_normalization_28[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_28 (Conv2D)               (None, 7, 7, 16)      57600       activation_28[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_23 (Concatenate)     (None, 7, 7, 416)     0           concatenate_22[0][0]             \n",
      "                                                                   conv2d_28[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_29 (BatchNor (None, 7, 7, 416)     1664        concatenate_23[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_29 (Activation)       (None, 7, 7, 416)     0           batch_normalization_29[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_29 (Conv2D)               (None, 7, 7, 16)      59904       activation_29[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_25 (Concatenate)     (None, 7, 7, 64)      0           conv2d_26[0][0]                  \n",
      "                                                                   conv2d_27[0][0]                  \n",
      "                                                                   conv2d_28[0][0]                  \n",
      "                                                                   conv2d_29[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_transpose_1 (Conv2DTransp (None, 14, 14, 64)    36928       concatenate_25[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_26 (Concatenate)     (None, 14, 14, 432)   0           conv2d_transpose_1[0][0]         \n",
      "                                                                   concatenate_20[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_30 (BatchNor (None, 14, 14, 432)   1728        concatenate_26[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_30 (Activation)       (None, 14, 14, 432)   0           batch_normalization_30[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_30 (Conv2D)               (None, 14, 14, 16)    62208       activation_30[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_27 (Concatenate)     (None, 14, 14, 448)   0           concatenate_26[0][0]             \n",
      "                                                                   conv2d_30[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_31 (BatchNor (None, 14, 14, 448)   1792        concatenate_27[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_31 (Activation)       (None, 14, 14, 448)   0           batch_normalization_31[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_31 (Conv2D)               (None, 14, 14, 16)    64512       activation_31[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_28 (Concatenate)     (None, 14, 14, 464)   0           concatenate_27[0][0]             \n",
      "                                                                   conv2d_31[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_32 (BatchNor (None, 14, 14, 464)   1856        concatenate_28[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_32 (Activation)       (None, 14, 14, 464)   0           batch_normalization_32[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_32 (Conv2D)               (None, 14, 14, 16)    66816       activation_32[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_29 (Concatenate)     (None, 14, 14, 480)   0           concatenate_28[0][0]             \n",
      "                                                                   conv2d_32[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_33 (BatchNor (None, 14, 14, 480)   1920        concatenate_29[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_33 (Activation)       (None, 14, 14, 480)   0           batch_normalization_33[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_33 (Conv2D)               (None, 14, 14, 16)    69120       activation_33[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_31 (Concatenate)     (None, 14, 14, 64)    0           conv2d_30[0][0]                  \n",
      "                                                                   conv2d_31[0][0]                  \n",
      "                                                                   conv2d_32[0][0]                  \n",
      "                                                                   conv2d_33[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_transpose_2 (Conv2DTransp (None, 28, 28, 64)    36928       concatenate_31[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_32 (Concatenate)     (None, 28, 28, 368)   0           conv2d_transpose_2[0][0]         \n",
      "                                                                   concatenate_16[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_34 (BatchNor (None, 28, 28, 368)   1472        concatenate_32[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_34 (Activation)       (None, 28, 28, 368)   0           batch_normalization_34[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_34 (Conv2D)               (None, 28, 28, 16)    52992       activation_34[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_33 (Concatenate)     (None, 28, 28, 384)   0           concatenate_32[0][0]             \n",
      "                                                                   conv2d_34[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_35 (BatchNor (None, 28, 28, 384)   1536        concatenate_33[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_35 (Activation)       (None, 28, 28, 384)   0           batch_normalization_35[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_35 (Conv2D)               (None, 28, 28, 16)    55296       activation_35[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_34 (Concatenate)     (None, 28, 28, 400)   0           concatenate_33[0][0]             \n",
      "                                                                   conv2d_35[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_36 (BatchNor (None, 28, 28, 400)   1600        concatenate_34[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_36 (Activation)       (None, 28, 28, 400)   0           batch_normalization_36[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_36 (Conv2D)               (None, 28, 28, 16)    57600       activation_36[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_35 (Concatenate)     (None, 28, 28, 416)   0           concatenate_34[0][0]             \n",
      "                                                                   conv2d_36[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_37 (BatchNor (None, 28, 28, 416)   1664        concatenate_35[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_37 (Activation)       (None, 28, 28, 416)   0           batch_normalization_37[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_37 (Conv2D)               (None, 28, 28, 16)    59904       activation_37[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_37 (Concatenate)     (None, 28, 28, 64)    0           conv2d_34[0][0]                  \n",
      "                                                                   conv2d_35[0][0]                  \n",
      "                                                                   conv2d_36[0][0]                  \n",
      "                                                                   conv2d_37[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_transpose_3 (Conv2DTransp (None, 56, 56, 64)    36928       concatenate_37[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_38 (Concatenate)     (None, 56, 56, 304)   0           conv2d_transpose_3[0][0]         \n",
      "                                                                   concatenate_12[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_38 (BatchNor (None, 56, 56, 304)   1216        concatenate_38[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_38 (Activation)       (None, 56, 56, 304)   0           batch_normalization_38[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_38 (Conv2D)               (None, 56, 56, 16)    43776       activation_38[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_39 (Concatenate)     (None, 56, 56, 320)   0           concatenate_38[0][0]             \n",
      "                                                                   conv2d_38[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_39 (BatchNor (None, 56, 56, 320)   1280        concatenate_39[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_39 (Activation)       (None, 56, 56, 320)   0           batch_normalization_39[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_39 (Conv2D)               (None, 56, 56, 16)    46080       activation_39[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_40 (Concatenate)     (None, 56, 56, 336)   0           concatenate_39[0][0]             \n",
      "                                                                   conv2d_39[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_40 (BatchNor (None, 56, 56, 336)   1344        concatenate_40[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_40 (Activation)       (None, 56, 56, 336)   0           batch_normalization_40[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_40 (Conv2D)               (None, 56, 56, 16)    48384       activation_40[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_41 (Concatenate)     (None, 56, 56, 352)   0           concatenate_40[0][0]             \n",
      "                                                                   conv2d_40[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_41 (BatchNor (None, 56, 56, 352)   1408        concatenate_41[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_41 (Activation)       (None, 56, 56, 352)   0           batch_normalization_41[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_41 (Conv2D)               (None, 56, 56, 16)    50688       activation_41[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_43 (Concatenate)     (None, 56, 56, 64)    0           conv2d_38[0][0]                  \n",
      "                                                                   conv2d_39[0][0]                  \n",
      "                                                                   conv2d_40[0][0]                  \n",
      "                                                                   conv2d_41[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_transpose_4 (Conv2DTransp (None, 112, 112, 64)  36928       concatenate_43[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_44 (Concatenate)     (None, 112, 112, 240) 0           conv2d_transpose_4[0][0]         \n",
      "                                                                   concatenate_8[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_42 (BatchNor (None, 112, 112, 240) 960         concatenate_44[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_42 (Activation)       (None, 112, 112, 240) 0           batch_normalization_42[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_42 (Conv2D)               (None, 112, 112, 16)  34560       activation_42[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_45 (Concatenate)     (None, 112, 112, 256) 0           concatenate_44[0][0]             \n",
      "                                                                   conv2d_42[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_43 (BatchNor (None, 112, 112, 256) 1024        concatenate_45[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_43 (Activation)       (None, 112, 112, 256) 0           batch_normalization_43[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_43 (Conv2D)               (None, 112, 112, 16)  36864       activation_43[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_46 (Concatenate)     (None, 112, 112, 272) 0           concatenate_45[0][0]             \n",
      "                                                                   conv2d_43[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_44 (BatchNor (None, 112, 112, 272) 1088        concatenate_46[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_44 (Activation)       (None, 112, 112, 272) 0           batch_normalization_44[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_44 (Conv2D)               (None, 112, 112, 16)  39168       activation_44[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_47 (Concatenate)     (None, 112, 112, 288) 0           concatenate_46[0][0]             \n",
      "                                                                   conv2d_44[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_45 (BatchNor (None, 112, 112, 288) 1152        concatenate_47[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_45 (Activation)       (None, 112, 112, 288) 0           batch_normalization_45[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_45 (Conv2D)               (None, 112, 112, 16)  41472       activation_45[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_49 (Concatenate)     (None, 112, 112, 64)  0           conv2d_42[0][0]                  \n",
      "                                                                   conv2d_43[0][0]                  \n",
      "                                                                   conv2d_44[0][0]                  \n",
      "                                                                   conv2d_45[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_transpose_5 (Conv2DTransp (None, 224, 224, 64)  36928       concatenate_49[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_50 (Concatenate)     (None, 224, 224, 176) 0           conv2d_transpose_5[0][0]         \n",
      "                                                                   concatenate_4[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_46 (BatchNor (None, 224, 224, 176) 704         concatenate_50[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_46 (Activation)       (None, 224, 224, 176) 0           batch_normalization_46[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_46 (Conv2D)               (None, 224, 224, 16)  25344       activation_46[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_51 (Concatenate)     (None, 224, 224, 192) 0           concatenate_50[0][0]             \n",
      "                                                                   conv2d_46[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_47 (BatchNor (None, 224, 224, 192) 768         concatenate_51[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_47 (Activation)       (None, 224, 224, 192) 0           batch_normalization_47[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_47 (Conv2D)               (None, 224, 224, 16)  27648       activation_47[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_52 (Concatenate)     (None, 224, 224, 208) 0           concatenate_51[0][0]             \n",
      "                                                                   conv2d_47[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_48 (BatchNor (None, 224, 224, 208) 832         concatenate_52[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_48 (Activation)       (None, 224, 224, 208) 0           batch_normalization_48[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_48 (Conv2D)               (None, 224, 224, 16)  29952       activation_48[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_53 (Concatenate)     (None, 224, 224, 224) 0           concatenate_52[0][0]             \n",
      "                                                                   conv2d_48[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "batch_normalization_49 (BatchNor (None, 224, 224, 224) 896         concatenate_53[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "activation_49 (Activation)       (None, 224, 224, 224) 0           batch_normalization_49[0][0]     \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_49 (Conv2D)               (None, 224, 224, 16)  32256       activation_49[0][0]              \n",
      "____________________________________________________________________________________________________\n",
      "concatenate_54 (Concatenate)     (None, 224, 224, 240) 0           concatenate_53[0][0]             \n",
      "                                                                   conv2d_49[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "conv2d_50 (Conv2D)               (None, 224, 224, 21)  5040        concatenate_54[0][0]             \n",
      "____________________________________________________________________________________________________\n",
      "reshape_1 (Reshape)              (None, 50176, 21)     0           conv2d_50[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "activation_50 (Activation)       (None, 50176, 21)     0           reshape_1[0][0]                  \n",
      "____________________________________________________________________________________________________\n",
      "reshape_2 (Reshape)              (None, 224, 224, 21)  0           activation_50[0][0]              \n",
      "====================================================================================================\n",
      "Total params: 2,319,680\n",
      "Trainable params: 2,293,024\n",
      "Non-trainable params: 26,656\n",
      "____________________________________________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "from keras_contrib.applications.densenet import DenseNetFCN\n",
    "model = DenseNetFCN(input_shape=(224, 224, 3), classes=21,\n",
    "                    include_top=True,\n",
    "                    weight_decay=2e-3)\n",
    "optimizer = keras.optimizers.Adam(1e-4)\n",
    "\n",
    "model.compile(optimizer=optimizer,\n",
    "                  loss='categorical_crossentropy',\n",
    "                  metrics=['accuracy'])\n",
    "\n",
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "model.fit_generator(\n",
    "    datagen.flow_from_imageset(\n",
    "        class_mode='categorical',\n",
    "        classes=21,\n",
    "        batch_size=1,\n",
    "        shuffle=True,\n",
    "        image_set_loader=train_loader),\n",
    "    steps_per_epoch=1112,\n",
    "    epochs=100,\n",
    "    validation_data=datagen.flow_from_imageset(\n",
    "        class_mode='categorical',\n",
    "        classes=21,\n",
    "        batch_size=1,\n",
    "        shuffle=True,\n",
    "        image_set_loader=val_loader),\n",
    "    validation_steps=1111,\n",
    "    verbose=1,\n",
    "    callbacks=[lr_reducer, early_stopper, csv_logger, checkpointer, nan_terminator, TQDMNotebookCallback()])\n",
    "\n",
    "model.save('output/fcn_densenet.h5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
